import 'package:flutter/foundation.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:rxdart/subjects.dart';

// Streams are created so that app can respond to notification-related events since the plugin is initialised in the `main` function
final BehaviorSubject<ReceivedNotification> didReceiveLocalNotificationSubject =
    BehaviorSubject<ReceivedNotification>();

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();

final BehaviorSubject<String> selectNotificationSubject = BehaviorSubject<String>();

class ReceivedNotification {
  final int id;
  final String title;
  final String body;
  final String payload;

  ReceivedNotification({
    @required this.id,
    @required this.title,
    @required this.body,
    @required this.payload,
  });
}

class LocalNotificationUtil {
  LocalNotificationUtil._() {
    _initNotificationConfig();
  }

  static LocalNotificationUtil _instance;

  static LocalNotificationUtil get instance {
    if (_instance == null) {
      _instance = LocalNotificationUtil._();
    }
    return _instance;
  }

  localNotify(String text, String payload) {
    _notification(text, payload);
  }

  void _notification(String text, String payload, {String body}) async {
    var androidPlatformChannelSpecifics = AndroidNotificationDetails(
        'house730_pms/im', 'house730_pms/im', 'house730_pms/im',
        importance: Importance.max, priority: Priority.high, ticker: 'ticker');
    var iOSPlatformChannelSpecifics = IOSNotificationDetails();
    var platformChannelSpecifics =
        NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
    await flutterLocalNotificationsPlugin.show(0, text, body, platformChannelSpecifics, payload: payload);
  }

  Future<void> _initNotificationConfig() async {
    // app_icon，对应android icon res名，且该icon必须在drawable中存在。
    var initializationSettingsAndroid = AndroidInitializationSettings('app_icon');
    // Note: permissions aren't requested here just to demonstrate that can be done later using the `requestPermissions()` method
    // of the `IOSFlutterLocalNotificationsPlugin` class
    var initializationSettingsIOS = IOSInitializationSettings(
        requestAlertPermission: false,
        requestBadgePermission: false,
        requestSoundPermission: false,
        onDidReceiveLocalNotification: (int id, String title, String body, String payload) async {
          didReceiveLocalNotificationSubject
              .add(ReceivedNotification(id: id, title: title, body: body, payload: payload));
        });
    var initializationSettings =
        InitializationSettings(android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
    flutterLocalNotificationsPlugin.initialize(initializationSettings, onSelectNotification: (String payload) async {
      if (payload != null) {
        debugPrint('notification payload: ' + payload);
      }
//          selectNotificationSubject.add(payload);
    });
  }
}
